<?php
/**
 * 问卷调查
 * 张名尊 20140511
 * 1、查询题目列表 、答案列表
 * 2、查询问卷列表
 * 3、添加编辑功能
 * 4、答案查询， 条件a、问卷，b、用户名
 * 
 */
  
class questionAction extends backendAction{
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('question');
    }
       
    public function _before_index(){
    
        $big_menu = array(
            'title' => '添加问卷',
            'iframe' => U('question/oppt',array('opttype'=>'add')),
            'id' => 'add',
            'width' => '500',
            'height' => '260'
        );
        $this->assign('big_menu', $big_menu);       
    }
    

    
    /**
     * 搜索
     */ 
    protected function _search() {
        $map = array();
        $pid = intval($this->_request('pid'));
        $map['pid'] = $pid;
        if($pid>0){
            $currentInfo = M('question')->where(array('id'=>$pid))->find();
            $this->assign('currentInfo',$currentInfo);
        }
        if($keyWord = $this->_request('keyWord', 'trim')){ 
            $map['name']  = array('like','%'.$keyWord.'%');
        }       
        $this->assign('search', array(
            'keyWord'=>$keyWord,
            'pid'=>$pid,
        ));
        return $map;
    }
    
    public function oppt(){
        $type = $this->_request('opttype');
        $this->assign('type',$type);

        $pid = $this->_get('pid', 'intval', 0);
        if ($pid) {
            $currentInfo = $this->_mod->where(array('id'=>$pid))->find();
            $spid  = $currentInfo['spid']; 
            $spid = $spid ? $spid.$pid : $pid;
            $info['spid'] = $spid;
            $this->assign('info', $info);
            $this->assign('currentInfo', $currentInfo);
        }  

        $this->$type();              
        $this->display();
    }

    /**
     * 入库数据整理
     */
    protected function _before_insert($data = '') {
        //检测分类是否存在
        if($this->_mod->name_exists($data['name'], $data['pid'])){
            $this->ajaxReturn(0, "已存在，请重新提交");
        }
        //生成spid
        $data['spid'] = $this->_mod->get_spid($data['pid']);
        $data['level'] = substr_count($data['spid'],'|');
        return $data;
    }

  
    /**
     * 修改提交对数据
     */
    protected function _before_update($data = '') {
        
        if ($this->_mod->name_exists($data['name'], $data['pid'], $data['id'])) {
            $this->ajaxReturn(0, "已存在，请重新提交");
        }
        $old_pid = $this->_mod->field('pid')->where(array('id'=>$data['id']))->find();
        if ($data['pid'] != $old_pid['pid']) {
            //不能把自己放到自己或者自己的子目录们下面
            $wp_spid_arr = $this->_mod->get_child_ids($data['id'], true);
            if (in_array($data['pid'], $wp_spid_arr)) {
                $this->ajaxReturn(0, L('cannot_move_to_child'));
            }
            //重新生成spid
            $data['spid'] = $this->_mod->get_spid($data['pid']);
        }
        return $data;
    }


    public function ajax_getchilds() {
        $id = $this->_get('id', 'intval');
        $max_level = $this->_request('max_level','intval',2);
        $return = $this->_mod->field('id,name')->where(array('pid'=>$id,'level'=>array('lt',$max_level)))->select();
        if ($return) {
            $this->ajaxReturn(1, L('operation_success'), $return);
        } else {
            $this->ajaxReturn(0, L('operation_failure'));
        }
    }
    
    
    /**
     * 取分类路径
     */ 
    public function ajax_get_spid(){
        $spid = '';
        $id = $this->_get('id', 'intval');  
        $spid = M('region')->where(array('id'=>$id))->getField('spid');
        if(!empty($spid)){
            $spid .= $id.'|';
        }
        echo trim($spid);
        exit();
    }         
}




?>